<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Withdrawals');
Globals::requireTable('Msg');
Globals::requireTable('Account');

class UserController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $WithdrawalsTable;
	protected $MsgTable;
	protected $AccountTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable          = new UserTable($config);
		$this->ProductTable       = new ProductTable($config);
		$this->OrderTable         = new OrderTable($config);
		$this->WithdrawalsTable   = new WithdrawalsTable($config);
		$this->MsgTable           = new MsgTable($config);
		$this->AccountTable       = new AccountTable($config);
	}
	
	/**
	 * 首页数据
	 */
	public function indexAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'orders'=>0,'messages'=>0,'accounts'=>0));
	        exit();
	    }
	    
	    $orders = $this->OrderTable->listCount(array('uid'=>$uid,'status>?'=>0));
	    $messages = $this->MsgTable->listCount(array('uid'=>$uid,'status'=>0));
	    $accounts = $this->AccountTable->listCount(array('uid'=>$uid));
	    echo json_encode(array('returncode'=>0,'orders'=>$orders,'messages'=>$messages,'accounts'=>$accounts));
	    exit();
	}
	
	/**
	 * 购买记录
	 * uid/token&machine_type
	 */
	public function orderlistAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $time = time();
	    $result = array();
	    $user = $this->UserTable->getRow($uid);
	    $orders = $this->OrderTable->listAll(array('uid'=>$uid,'status>?'=>0),'id desc');
	    foreach ($orders as $v){
	        $product = $this->ProductTable->getRow($v['pid']);
	        $tmp['orderid'] = $v['orderid'];
	        $tmp['name'] = $product['name'].$v['days'].'天试用期';
	        $tmp['money'] = $v['money'];
	        $tmp['wechat'] = $user['wechatID'];
	        $tmp['dayend'] = $v['pasttime'] < $time ? '已过期' : date('Y-m-d',$v['pasttime']);
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	/**
	 * 注册
	 */
	public function signAction(){
	    $token = $this->getParam('token');
	    $machine_type= $this->getIntParam('machine_type');
	    if(empty($token)){
	        echo json_encode(array('returncode'=>-1,'uid'=>0));
	        exit();
	    }
	    
	    $user = $this->UserTable->getRow(array('token'=>$token));
	    if($user['id'] < 1){
	        $data['nickname'] = substr($token, 3,6).rand(1000, 9999);
	        $data['username'] = substr($token, 3,6).rand(1000, 9999);
	        $data['token'] = $token;
	        $data['machine_type'] = (int)$machine_type;
	        $user['id'] = $this->UserTable->add($data,true);
	    }
	    
	    echo json_encode(array('returncode'=>0,'uid'=>$user['id']));
	    exit();
	}
	
	/**
	 * 申请提现页面
	 */
	public function withdrawalsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'money'=>0));
	        exit();
	    }
	    
	    $money = 0;
	    $orders = $this->OrderTable->listAll(array('uid'=>$uid,'status'=>2));
	    foreach ($orders as $v){
	        $money += $v['money'];
	    }
	    echo json_encode(array('returncode'=>0,'money'=>$money));
	    exit();
	}
	
	/**
	 * 申请提现功能
	 */
	public function dowithdrawalsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $alipay = $this->getParam('alipay');
	    if(empty($alipay)){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $money = 0;
	    $orderids = array();
	    $orders = $this->OrderTable->listAll(array('uid'=>$uid,'status'=>2));
        if(count($orders) < 1){
	        echo json_encode(array('returncode'=>-3));
	        exit();
	    }
	    
	    foreach ($orders as $v){
	        $money += $v['money'];
	        $orderids[] = $v['orderid'];
	        $this->OrderTable->update(array('status'=>3),$v['id']);
	    }
	    
	    $data['uid'] = $uid;
	    $data['money'] = $money;
	    $data['createdate'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $data['orderids'] = implode(',', $orderids);
	    $data['alipay'] = $alipay;
	    $this->WithdrawalsTable->add($data);
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * v级列表
	 */
	public function vlistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $list = $listvalue = array();
	    $orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
	    foreach ($orders as $v){
	        $listvalue[$v['pid']] += $v['money'];
	    }
	    foreach ($listvalue as $k=>$v){
	        $product = $this->ProductTable->getRow($k);
	        $tmp['name'] = $product['name'];
	        $tmp['v'] = $v;
	        $list[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$list));
	    exit();
	}
	
	/**
	 * 收入列表
	 */
    public function incomeAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array(),'money'=>0));
            exit();
        }
         
        $list = array();
        $money = 0;
        $orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
        foreach ($orders as $v){
            $account = $this->AccountTable->getRow($v['aid']);
            $tmp['name'] = $account['username'].'转让';
            $tmp['money'] = $v['money'];
            $list[] = $tmp;
            $money += $v['money'];
        }
        echo json_encode(array('returncode'=>0,'list'=>$list,'money'=>$money));
        exit();
    }
	
	/**
	 * 用户中心
	 */
    public function ucenterAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'v'=>0,'money'=>0,'income'=>0,'username'=>''));
            exit();
        }
        
        $money = $income = 0;
        $orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
        foreach ($orders as $v){
            $income += $v['money'];
            if($v['status'] > 1){
                $money += $v['money'];
            }
        }
        $user = $this->UserTable->getRow($uid);
        echo json_encode(array('returncode'=>0,'v'=>$income,'money'=>$money,'income'=>$income,'username'=>$user['nickname']));
        exit();
    }
	
	/**
	 * 消息列表
	 */
    public function msglistAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array()));
            exit();
        }
        
        $list = array();
        $msgs = $this->MsgTable->listAll(array('uid'=>$uid));
        foreach ($msgs as $v){
            $tmp['message'] = $v['msg'];
            $tmp['time'] = $v['createdate'];
            $tmp['status'] = $v['status'];
            $tmp['id'] = $v['id'];
            $list[] = $tmp;
        }
        echo json_encode(array('returncode'=>0,'list'=>$list));
        exit();
    }
	
	/**
	 * 消息详情
	 */
    public function msgdetailAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'message'=>''));
            exit();
        }
        
        $msgid = $this->getIntParam('messageid');
        if($msgid == 0){
            echo json_encode(array('returncode'=>-2,'message'=>''));
            exit();
        }
        
        $message = $this->MsgTable->getRow($msgid);
        $this->MsgTable->update(array('status'=>1),$msgid);
        echo json_encode(array('returncode'=>0,'message'=>$message['msg']));
        exit();
    }
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('UserController', 'Controller');
